package com.subsequent.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsequent.mapper.PharmaciesMapper;
import com.subsequent.pojo.Pharmacies;
import com.subsequent.service.PharmaciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

@Service
public class PharmaciesServiceImpl extends ServiceImpl<PharmaciesMapper, Pharmacies> implements PharmaciesService {
    @Autowired
    private PharmaciesMapper pharmaciesMapper;

    @Override
    public Page<Pharmacies> pageQuery(int pageNum, int pageSize, String pharmacyName, String pharmacyType, Integer status, Date startTime, Date endTime) {
        int offset = (pageNum - 1) * pageSize;
        List<Pharmacies> list = pharmaciesMapper.selectPharmaciesByCondition(pharmacyName, pharmacyType, status, startTime, endTime, offset, pageSize);
        int total = pharmaciesMapper.countPharmaciesByCondition(pharmacyName, pharmacyType, status, startTime, endTime);
        Page<Pharmacies> page = new Page<>(pageNum, pageSize, total);
        page.setRecords(list);
        return page;
    }

    @Override
    public boolean changeStatus(Integer pharmacyId, Integer status) {
        Pharmacies pharmacy = this.getById(pharmacyId);
        if (pharmacy == null) return false;
        pharmacy.setStatus(status);
        return this.updateById(pharmacy);
    }

    @Override
    public List<String> getAllPharmacyTypes() {
        return pharmaciesMapper.selectAllPharmacyTypes();
    }
}